import java.util.*;
import java.util.Map;

public class Sort {

    public static void swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }


    //直接插入排序
    public static void invertSort(int[] array) {
        for(int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = 0;
            for(j = i - 1; j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
                array[j] = tmp;
            }
        }
    }


    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length / 2;
        while(gap != 0) {
            shell(array, gap);
            gap /= 2;
        }
        invertSort(array);
    }

    private static void shell(int[] array, int gap) {
        for(int i = gap; i < array.length; i++) {
            int j = 0;
            for(j = i - gap; j >= 0; j -= gap) {
                if(array[j] > array[i]) {
                    swap(array, j, i);
                }else {
                    break;
                }
            }
        }
    }

    //直接选择排序
    public static void selectSort(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while(left < right) {
            int max = left;
            int min = left;
            for(int i = left + 1; i <= right; i++) {
                if(array[i] > array[max]) {
                    max = i;
                }
                if(array[i] < array[min]) {
                    min = i;
                }
            }
            swap(array, left, min);
            //如果没有这句话此时array[max]已经被上一步给交换走了，所以需要重新记录最大值的位置
            if(max == left) {
                max = min;
            }
            swap(array, right,max);
            left++;
            right--;
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] array) {

        for(int i = 0; i < array.length - 1; i++) {
            boolean flag = true;
            for(int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flag = false;
                }
            }
            if(flag) {
                break;
            }
        }
    }

    private static int middleNumber(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if(array[left] < array[right]) {
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[left]) {
                return right;
            }else if(array[mid] > array[right]) {
                return left;
            }else {
                return mid;
            }
        }
    }
    //快速排序
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }
    private static void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }

        //优化一
        if(right - left + 1 <= 15) {
            invertSort(array);
            return;
        }

        //优化二
        int mid = middleNumber(array, left, right);
        swap(array, left, mid);
        int ret = partition(array, left, right);
        quick(array, left, ret - 1);
        quick(array, ret + 1, right);

    }

    //Hoare法：
    private static int partitionHoare(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while(left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }

    //挖坑法：
    private static int partitionHole(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while(left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //双向指针法：
    private static int partition(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while(cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, left, cur);
            }
            cur++;
        }
        swap(array, prev, left);
        return prev;
    }

    public static void quickSortNor(int[] array) {
        int left = 0;
        int right = array.length - 1;
        int pivot = partitionHoare(array, left, right);
        Stack<Integer> s = new Stack<>();
        if(pivot - 1 > left) {
            s.push(left);
            s.push(pivot - 1);
        }
        if(pivot + 1 < right) {
            s.push(pivot + 1);
            s.push(right);
        }
        while(!s.isEmpty()) {
            right = s.pop();
            left = s.pop();
            pivot = partitionHoare(array, left, right);
            if(pivot - 1 > left) {
                s.push(left);
                s.push(pivot - 1);
            }
            if(pivot + 1 < right) {
                s.push(pivot + 1);
                s.push(right);
            }
        }
    }
//    public static void main1(String[] args) {
////        int[] array = new int[] {15,10,9,25,18};
////        quickSortNor(array);
////
////        System.out.println(Arrays.toString(array));
//        Map m = new Map();
//        m.insert(25);
//        m.insert(8);
//        m.insert(35);
//        m.insert(16);
//        m.insert(5);
//        m.insert(23);
//        System.out.println(m.search(23));
//
//        m.remove(25);
//        System.out.println();
//    }

    public static void main1(String[] args) {
        Map<Integer, Integer> map1 = new TreeMap<>();
        map1.put(1,15);
        map1.put(1,8);
        map1.put(2,13);
        map1.put(9,41);
        map1.put(5,35);
        map1.put(5,65);
        map1.put(8,26);

        Set<Integer> set1 = new TreeSet<>();
        set1 = map1.keySet();
        Collection<Integer> collection = new TreeSet<>();
        collection = map1.values();
        System.out.println();

        Set<Map.Entry<Integer,Integer>> set2 = map1.entrySet();
        for(Map.Entry<Integer,Integer> entry : set2) {
            System.out.println(entry.getKey());
        }

    }

    public static void main(String[] args) {
        Map<Integer,Integer> map1 = new HashMap<>();
        map1.put(1,15);
        map1.put(8,35);
        map1.put(9,8);
        map1.put(5,19);
        map1.put(4,26);
        System.out.println();
    }
}
